package com.jspgou.cms.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.cms.dao.ConsultDao;
import com.jspgou.cms.entity.Consult;
import com.jspgou.cms.manager.ConsultMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.ShopMemberMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;

@Service
@Transactional
public class ConsultMngImpl implements ConsultMng {
	@Transactional(readOnly = true)
	public Consult findById(Long id) {
		Consult entity = dao.findById(id);
		return entity;
	}

	public Consult saveOrUpdate(Long productId,Long storeId,String content,String consultType,Long memberId){
		Consult bean=new Consult();
		bean.setConsult(content);
		
		//添加字段，标记咨询类型
		bean.setConsultType(consultType);
		
		bean.setMember(memberMng.findById(memberId));
		bean.setConsultTime(new Date());
		bean.setProduct(productMng.findById(productId));
		if(storeId!=null){
			bean.setStore(storeMng.findById(storeId));
		}
		bean.setDisable(false);
		Consult consult=dao.getSameConsult(memberId);
		Long time=System.currentTimeMillis();
		if(consult==null){
			return dao.saveOrUpdate(bean);
		}else{
			if((time-consult.getConsultTime().getTime())/1000<30){
				return null;
			}else{
				return dao.saveOrUpdate(bean);
			}
		}
		
	}
	
	public Pagination getPage(Long memberId,Long storeId,int pageNo,int pageSize,boolean cache){
		return dao.getPage(memberId, storeId,pageNo, pageSize, cache);
	}
	
	public Pagination getPage(Long productId,Long memberId,Long storeId,String consultType,String userName,String productName,
			Date startTime,Date endTime,int pageNo,int pageSize,Boolean cache){
		return dao.getPage(productId,memberId,storeId,consultType,userName,productName,startTime,endTime,pageNo, pageSize, cache);
	}

	public Consult update(Consult entity) {
		Updater<Consult> updater = new Updater<Consult>(entity);
		entity = dao.updateByUpdater(updater);
		return entity;
	}

	public Consult deleteById(Long id) {
		Consult bean = dao.deleteById(id);
		return bean;
	}
	
	//根据咨询类型删除记录
	public List<Consult> deleteByType(String consultType) {
		List<Consult> consultList=dao.deleteByType(consultType);
		return consultList;
	}
	//根据咨询类型获取consult
	public List<Consult> findByType(Long productId,String consultType){
		return dao.findByType(productId,consultType);
	}
	
	public Consult[] deleteByIds(Long[] ids){
		Consult[] beans = new Consult[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	
	@Autowired
	private ConsultDao dao;
	@Autowired
	private StoreMng storeMng;
	@Autowired
	private ProductMng productMng;
	@Autowired
	private ShopMemberMng memberMng;
	
}